package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.asymmetric.RSA;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyRongHaiJinFuData;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.Map;

/**
 * 安心分期-融海金服-单选-35
 *
 * @author WJB
 */
@ApiPlan(productId = "265", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMd5RongHaiJinFu implements ApiOrgPhoneMaskConvertHandler {

    private static final String KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzbTdtagDz2vX3uFCeoJQEslv+5ikvFnWgwxowVed9DbgiOG/Xt+QTNOaI3c5PoLN56bRfmdHI7dSYGl5bbsdg9xXdEjMBqQ7ISrNHeBBIQ5IbsIMkQjASR4TITsG4WljoYVDNcvdc/JDR1qzBnDUXdaW3qaQ3nbi3gl8evOoqYk7Y8J/c7EnXfSLediXUYeAqeVF9G48LLzHb1LPY4lxEhOGFOcvDEinCTnKZ61ZGlQkYJuYr/Tg9ryrOxJBWWy6zL8hcCFD9t1cjEsDq0SwgfNPkNldXSxgK3uMTdePBWPGobcHAM3McZyKp3gVU5686U4Zj8YwXIJeFsbZ6QACyQIDAQAB";
    private static final String CHANNEL_ID = "553befa9bc7f448685c5a279525d813b";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataChangeMessage) throws Exception {
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelId", CHANNEL_ID);
            newMap.put("customerPhone", apiDataChangeMessage.getPhoneMask());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        boolean status = jsonObject.getBoolean("status");
        Integer code = jsonObject.getInteger("code");
        if (!(status && code == 0)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        return RespOrgPhoneMaskDataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter ApiApplyMessage) throws Exception {
        //  拓展判断信息
        com.ts.api.module.api.convert.core.utils.ApiFieldConverter apiFieldConverter = com.ts.api.module.api.convert.core.utils.ApiFieldConverter.create(ApiApplyMessage);

        OrgApplyRongHaiJinFuData apply = new OrgApplyRongHaiJinFuData();
        apply.setCustomerName(ApiApplyMessage.getUserName());
        apply.setCustomerPhone(ApiApplyMessage.getPhone());

        //  性别
        Integer sex = ChooseEq.create()
                .when(ApiApplyMessage.getSex(), "0", 0)
                .when(ApiApplyMessage.getSex(), "1", 1)
                .end(2);
        apply.setSex(sex);
        apply.setCity(ApiApplyMessage.getAreaCode());
        //  年龄
        apply.setAge(apiFieldConverter.getAgeInt());
        //  学历
        if (ValidParam.isNotEmpty(ApiApplyMessage.getEducationAttainment())) {
            String text = ChooseEq.create()
                .when(ApiApplyMessage.getEducationAttainment(), "1", "1")
                .when(ApiApplyMessage.getEducationAttainment(), "2", "1")
                .when(ApiApplyMessage.getEducationAttainment(), "3", "2")
                .when(ApiApplyMessage.getEducationAttainment(), "4", "3")
                .when(ApiApplyMessage.getEducationAttainment(), "5", "4")
                .when(ApiApplyMessage.getEducationAttainment(), "6", "5")
            .end();
            apply.setEducation(text);
        }
        //  逾期
        if (ValidParam.isNotEmpty(ApiApplyMessage.getOverdue())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getOverdue(), "1", "1")
                    .when(ApiApplyMessage.getOverdue(), "2", "2")
                    .when(ApiApplyMessage.getOverdue(), "3", "3")
                    .end();
            apply.setCreditSituation(text);
        }
        //  贷款期限
        if (ValidParam.isNotEmpty(ApiApplyMessage.getLoanTerm())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getLoanTerm(), "1", "1")
                    .when(ApiApplyMessage.getLoanTerm(), "2", "2")
                    .when(ApiApplyMessage.getLoanTerm(), "3", "4")
                    .when(ApiApplyMessage.getLoanTerm(), "4", "5")
                    .when(ApiApplyMessage.getLoanTerm(), "5", "6")
                    .when(ApiApplyMessage.getLoanTerm(), "6", "6")
                    .end();
            apply.setLengthOfMaturity(text);
        }
        //  信用卡额度
        if (ValidParam.isNotEmpty(ApiApplyMessage.getCreditLimit())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getCreditLimit(), "1", "1")
                    .when(ApiApplyMessage.getCreditLimit(), "2", "2")
                    .when(ApiApplyMessage.getCreditLimit(), "3", "3")
                    .when(ApiApplyMessage.getCreditLimit(), "4", "4")
                    .when(ApiApplyMessage.getCreditLimit(), "5", "5")
                    .when(ApiApplyMessage.getCreditLimit(), "6", "2")
                    .end();
            apply.setCreditCardLimit(text);
        }
        //  公积金
        if (ValidParam.isNotEmpty(ApiApplyMessage.getProvidentFund())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getProvidentFund(), "1", "1")
                    .when(ApiApplyMessage.getProvidentFund(), "2", "3")
                    .when(ApiApplyMessage.getProvidentFund(), "3", "4")
                    .when(ApiApplyMessage.getProvidentFund(), "4", "2")
                    .end();
            apply.setAccumulationFund(text);
        }
        //  社保
        if (ValidParam.isNotEmpty(ApiApplyMessage.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getSocialSecurity(), "1", "1")
                    .when(ApiApplyMessage.getSocialSecurity(), "2", "3")
                    .when(ApiApplyMessage.getSocialSecurity(), "3", "4")
                    .when(ApiApplyMessage.getSocialSecurity(), "4", "2")
                    .end();
            apply.setSocialSecurity(text);
        }
        //  芝麻分
        if (ValidParam.isNotEmpty(ApiApplyMessage.getSesameCredit())) {
            String zhima = ChooseEq.create()
                    .when(ApiApplyMessage.getSesameCredit(), "1", "3")
                    .when(ApiApplyMessage.getSesameCredit(), "2", "4")
                    .when(ApiApplyMessage.getSesameCredit(), "3", "5")
                    .when(ApiApplyMessage.getSesameCredit(), "4", "6")
                    .end();
            apply.setSesameSeeds(zhima);
        }
        //  房产
        if (ValidParam.isNotEmpty(ApiApplyMessage.getRealEstateInfo())) {
            String house = ChooseEq.create()
                    .when(ApiApplyMessage.getRealEstateInfo(), "1", "1")
                    .when(ApiApplyMessage.getRealEstateInfo(), "2", "2")
                    .when(ApiApplyMessage.getRealEstateInfo(), "3", "2")
                    .when(ApiApplyMessage.getRealEstateInfo(), "4", "2")
                    .when(ApiApplyMessage.getRealEstateInfo(), "5", "3")
                    .when(ApiApplyMessage.getRealEstateInfo(), "6", "4")
                    .end();
            apply.setRoomCondition(house);
        }
        //  车产
        if (ValidParam.isNotEmpty(ApiApplyMessage.getCarInfo())) {
            String car = ChooseEq.create()
                    .when(ApiApplyMessage.getCarInfo(), "1", "1")
                    .when(ApiApplyMessage.getCarInfo(), "2", "2")
                    .when(ApiApplyMessage.getCarInfo(), "3", "2")
                    .when(ApiApplyMessage.getCarInfo(), "4", "2")
                    .end();
            apply.setVehicleCondition(car);
        }
        //  花呗额度
        if (ValidParam.isNotEmpty(ApiApplyMessage.getHbLimit())) {
            String huaBei = ChooseEq.create()
                    .when(ApiApplyMessage.getHbLimit(), "1", "2")
                    .when(ApiApplyMessage.getHbLimit(), "2", "4")
                    .when(ApiApplyMessage.getHbLimit(), "3", "5")
                    .when(ApiApplyMessage.getHbLimit(), "4", "6")
                    .when(ApiApplyMessage.getHbLimit(), "5", "7")
                    .end();
            apply.setHuaBei(huaBei);
        }
        //  白条额度
        if (ValidParam.isNotEmpty(ApiApplyMessage.getBtLimit())) {
            String baiTiao = ChooseEq.create()
                    .when(ApiApplyMessage.getBtLimit(), "1", "2")
                    .when(ApiApplyMessage.getBtLimit(), "2", "4")
                    .when(ApiApplyMessage.getBtLimit(), "3", "5")
                    .when(ApiApplyMessage.getBtLimit(), "4", "6")
                    .when(ApiApplyMessage.getBtLimit(), "5", "7")
                    .end();
            apply.setBaiTiao(baiTiao);
        }
        //  保单
        if (ValidParam.isNotEmpty(ApiApplyMessage.getInsurancePolicy())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getInsurancePolicy(), "1", "1")
                    .when(ApiApplyMessage.getInsurancePolicy(), "2", "3")
                    .when(ApiApplyMessage.getInsurancePolicy(), "3", "4")
                    .when(ApiApplyMessage.getInsurancePolicy(), "4", "2")
                    .end();
            apply.setInsurancePolicy(text);
        }
        //  职业
        if (ValidParam.isNotEmpty(ApiApplyMessage.getCareer())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getCareer(), "1", "1")
                    .when(ApiApplyMessage.getCareer(), "2", "8")
                    .when(ApiApplyMessage.getCareer(), "3", "5")
                    .when(ApiApplyMessage.getCareer(), "4", "6")
                    .end();
            apply.setOccupation(text);
        }
        //  贷款额度
        apply.setLoanAmount(apiFieldConverter.getLoanAmountInt());
        //   月收入
        if (ValidParam.isNotEmpty(ApiApplyMessage.getMonthlySalary())) {
            String monthlySalary = ChooseEq.create()
                    .when(ApiApplyMessage.getMonthlySalary(), "1", "1")
                    .when(ApiApplyMessage.getMonthlySalary(), "2", "2")
                    .when(ApiApplyMessage.getMonthlySalary(), "3", "3")
                    .when(ApiApplyMessage.getMonthlySalary(), "4", "4")
                    .when(ApiApplyMessage.getMonthlySalary(), "5", "5")
                    .end();
            apply.setMonthlyIncome(monthlySalary);
        }
        // 营业执照
        if (ValidParam.isNotEmpty(ApiApplyMessage.getBizLicenseYear())) {
            String text = ChooseEq.create()
                    .when(ApiApplyMessage.getBizLicenseYear(), "1", "2")
                    .when(ApiApplyMessage.getBizLicenseYear(), "2", "3")
                    .when(ApiApplyMessage.getBizLicenseYear(), "3", "4")
                    .when(ApiApplyMessage.getBizLicenseYear(), "4", "4")
                    .end();
            apply.setEntrepreneurLicense(text);
        }
        //  加密数据
        RSA rsa = new RSA(KEY, null);
        String data = rsa.encryptToBase64Str(JsonUtils.toJson(apply));
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelId", CHANNEL_ID);
            newMap.put("customerInfo", data);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        boolean status = jsonObject.getBoolean("status");
        Integer code = jsonObject.getInteger("code");
        if(!(status && code == 0)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
